$(function(){
	anime_play_index.run();
});

var anime_play_index = {
	run: function(){
		this._init_();			
		this._start_();
		this._bind_events();
	},
	
	_init_: function(){
		this.showId = $("#showId").val() || "";
		
		var loc = location.href;
		this.category = loc.substring(loc.indexOf("category") + 9);
	},
	
	_start_: function(){
		this._highlight_nav();

		var ua = navigator.userAgent;
		if(ua.indexOf("IE") > 0 ){
			$(".ie_player").show();
		}else{
			$(".ff_player").show();
		}
		
		var loc = location.href;
		var index = loc.indexOf("#");
		if(index > 0){
			var params = loc.substring(index + 1);
			this._highlight_genre(params);
		}
		
		main._play_close_side();
		
		if(this.category != "all"){
			$("#midlle_div").hide();
		}
		
		//this._get_relate_anime();
	},
	
	_bind_events: function(){
		$("#forward").live("click", function() {
			var divId = $(this).attr("divId");
			$(".tv_sList").css("display", "none");
			$("#" + divId).css("display", "block");
		});
		
		$("#backward").live("click", function() {
			var divId = $(this).attr("divId");
			$(".tv_sList").css("display", "none");
			$("#" + divId).css("display", "block");
		});
		
		$("#displayEpisode").live("click", function() {
			$("#episodeDetail").toggle();
		});
		
		//genre
		$("#genres_ul > li > a").click(function(){
			var $this = $(this);
			var g = $this.attr("genre");
			location.href = main.contextPath + "/world/anime/all#genre=" + g; 
			return false;
		});
		
		var locaIndex = location.href.indexOf("#"); 
		var localUrl = location.href;
		if(locaIndex > 0){
			localUrl = localUrl.substring(0,locaIndex);
		}
		$("#fb-comments").attr("data-href", localUrl);
	},
	
	_get_relate_anime: function(){
		var url = main.contextPath + "/world/anime/relate/" + this.showId;
		$.ajax({
			url: url,
			cache: false,
			beforeSend: function(){
				$(".loadingBox").show();
			},
			success: function(result){
				$("#other_video").html(result.trim());
				$(".loadingBox").hide();
			}
		});
	},
	
	_highlight_nav: function(){
		var $li = $("#side_menu > ul > li");
		if(this.category == "updated"){
			$li.eq(2).addClass("curr");
		}else if(this.category == "simulacasts"){
			$li.eq(1).addClass("curr");
		}else{
			$li.eq(0).addClass("curr");
		}
	},
	
	_highlight_genre: function(params){
		var paramArr = params.split("&");
		for(var i=0; i<paramArr.length;i++){
			if (paramArr[i].indexOf("genre") >= 0) {
				var value = paramArr[i].substring(paramArr[i].indexOf("=") + 1);
				$("#genres_ul > li > a").removeClass("selected");
				$("#genres_ul > li > a").each(function() {
					if ($(this).attr("genre") == decodeURI(value)) {
						$(this).addClass("selected");
					}
				});
			}
		}
	}
};